<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>系统日志</title>
    <link rel="stylesheet" href="/assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="/assets/module/admin.css?v=315"/>
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>

<!-- 页面加载loading -->
<div class="page-loading">
    <div class="ball-loader">
        <span></span><span></span><span></span><span></span>
    </div>
</div>

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">用户名：</label>
                        <div class="layui-input-inline mr0">
                            <input name="username" class="layui-input" id="demoReload" placeholder="请输入查询用户名">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchLog" data-type="reload" lay-submit >
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="online" lay-filter="tableLog"></table>
        </div>
    </div>
</div>
<script type="text/html" id="online-option">
    <shiro:hasPermission name="online:kick">
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">踢出</a>
    </shiro:hasPermission>
    <shiro:lacksPermission name="online:kick">
        <span class="layui-btn layui-btn-danger layui-btn-xs" >无权限</span>
    </shiro:lacksPermission>
</script>
<script type="text/html" id="online-username">
    <div>
        {{d.username}}
        {{#
        if (d.current) {
        }}
        <span class="layui-badge layui-bg-green">当前用户</span>
        {{#
        }
        }}
    </div>
</script>
<script type="text/html" id="online-status">
    {{#
    var status = {
    1: {title: '在线', color: 'green'},
    0: {title: '离线', color: 'volcano'}
    }[d.status];
    }}
    <span class="layui-badge layui-bg-{{status.color}}">{{ status.title }}</span>
</script>
<script type="text/html" id="toolbarDemo">
    <shiro:hasPermission name="login_log:delete">
        <button class="layui-btn layui-btn-sm" lay-event="delete">踢出选中的用户</button>
    </shiro:hasPermission>
    <shiro:lacksPermission name="login_log:delete">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-danger" disabled>无权限</button>
        </div>
    </shiro:lacksPermission>
</script>
<!-- js部分 -->
<script type="text/javascript" src="/assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="/assets/js/common.js?v=315"></script>
</body>
<script>

    layui.use(['table','form','notice'], function(){
        var table = layui.table,
        form=layui.form,
        notice=layui.notice;

        var talbeIns=table.render({
            elem: '#online',
            url:'/session/list'
            ,toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
            defaultToolbar: [ 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '刷新'
                ,layEvent: 'reset'
                ,icon: 'layui-icon-refresh'
            }],
            cols: [
                [
                    {type: 'checkbox', fixed: 'left',align:'center'},
                    {title: '用户名', templet: '#online-username',align:'center'},
                    {title: '登录时间',field: 'startTimestamp',sort: true},
                    {title: '最后访问时间',field: 'lastAccessTime',sort: true},
                    {title: 'IP地址',field: 'host'},
                    {title: '登录地点',field: 'location'},
                    {title: '状态', templet: '#online-status',align:'center'},
                    {fixed: 'right',title: '操作', toolbar: '#online-option',align:'center'}
                ]
            ]
        });
        //表单刷新
        var $ = layui.$, active = {
            reload: function(){
                var demoReload = $('#demoReload');
                var timeArea = $("#timearea").val();
                var startTime = "";
                var endTime = "";
                if (timeArea) {
                    startTime = timeArea.split(" - ")[0];//开始时间
                    endTime = timeArea.split(" - ")[1];//结束时间
                }
                //执行重载
                table.reload('tableLog', {
                    where: {
                        username: demoReload.val(),
                    }
                }, 'data');
            }
        };
        //点击搜索按钮
        form.on('submit(formSubSearchLog)',function (data) {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        })

        //监听行工具事件
        table.on('tool(tableLog)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('确定将该用户踢出？',{icon:3, title:'提示信息',skin:'layui-layer-admin'}, function (index) {
                    if (data.current) {//判断是否是当前用户，如果是就退出
                        location.href="/logout"
                    } else {//如果不是踢出
                        $.get("/session/delete/" + data.id, {}, function () {
                            talbeIns.reload();
                            layer.close(index);
                            notice.msg("踢出用户成功",{icon:1,timeout:2000,animateInside:true,audio:'1'});
                        });
                    }
                });
            }
        });

        table.on('toolbar(tableLog)',function (obj) {
            if (obj.event==='reset'){
                talbeIns.reload();
            }
            if (obj.event==='delete'){
                var checkStats=table.checkStatus(obj.config.id);
                if (!checkStats.data.length)
                    notice.msg("请选择要踢出的用户！！",{icon:5,timeout:2000,animateInside:true,audio:'1'});
                else{
                    layer.confirm('确定要踢出多个用户？（不会退出当前用户）',{icon:3, title:'提示信息',skin:'layui-layer-admin'}, function (index) {
                        layui.each(checkStats.data,function (key,item) {
                            if(!item.current){
                                $.get("/session/delete/" + item.id, {}, function () {
                                });
                            }
                        });
                        layer.close(index);
                        talbeIns.reload();
                        notice.msg("踢出用户成功",{icon:1,timeout:2000,animateInside:true,audio:'1'});
                    });
                }
            }
        });

    });
</script>

</html>